package com.e3.basdoc.tax.client;

import java.util.ArrayList;
import java.util.List;

import com.e3.basdoc.tax.model.TaxModel;
import com.e3.template.tgf.GFHelper;
import com.e3.web.widget.client.MyFormFieldFactory;
import com.extjs.gxt.ui.client.widget.form.Field;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.RowNumberer;

public class TaxHelper extends GFHelper<TaxModel>{
	
	public static TaxHelper INSTANCE = new TaxHelper();
	
	private String[] notNullFields = {TaxModel.CODE,TaxModel.NAME,TaxModel.RATE};

	public List<Field<?>> getFormFields(){
		String[] fNames = {TaxModel.ID,TaxModel.CODE,TaxModel.NAME,TaxModel.RATE};
		List<Field<?>> fields = new ArrayList<Field<?>>(fNames.length);
		for (String name:fNames) {
			Field<?> field = createFormField(name);
			fields.add(field);
		}
		return fields;
	}
	
	public Field<?> createFormField(String name){
		Field<?> formField = null;
		String dispName = getFieldDisplayName(name);
		boolean isNotNull = isNotNullField(name);
		if(name.equals(TaxModel.ID)){
			formField = MyFormFieldFactory.createIdField(name);
		}		
		else if(name.equals(TaxModel.CODE)){
			formField = MyFormFieldFactory.createStringField(name,dispName,10,isNotNull);
		}
		else if(name.equals(TaxModel.NAME)){
			formField = MyFormFieldFactory.createStringField(name,dispName,20,isNotNull);
		}
		else if(name.equals(TaxModel.RATE)){
			formField = MyFormFieldFactory.createDoubleField(name, dispName, 2,6,isNotNull);
		}
		return formField;
	}
	
	public List<ColumnConfig> getGridColumnConfigs(){
		List<ColumnConfig> configs = new ArrayList<ColumnConfig>();
		RowNumberer numberer = new RowNumberer();   
		configs.add(numberer);
		
		ColumnConfig column = new ColumnConfig();
		String name = TaxModel.CODE;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);  
	    
	    column = new ColumnConfig();
		name = TaxModel.NAME;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);  
	    
	    column = new ColumnConfig();
		name = TaxModel.RATE;
	    column.setId(name);   
	    column.setHeader(getFieldDisplayName(name));   
	    column.setWidth(200);   
	    configs.add(column);
		    
	    return configs;
	}

	
	public String getFieldDisplayName(String code) {
		return TaxFieldsConstant.INSTANCE.getString(code);
	}

	@Override
	public String[] getNotNullFieldNames() {
		return notNullFields;
	}

	@Override
	public TaxModel createModelInstance() {
		return new TaxModel();
	}
}
